package org.stvd.controller.openim;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.SecurityUserHolder;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.common.utils.StringsUtil;
import org.stvd.common.utils.openim.OpenImApi;
import org.stvd.controller.BaseController;
import org.stvd.entities.admin.UserLogin;
import org.stvd.service.admin.UsersService;

import com.alibaba.fastjson.JSONObject;

/**
 * @Title OpenImInfoController
 * @Destribution 即时通讯
 * @author zhg5482
 * @date 2022年05月10号
 */
@Controller
@RequestMapping(value = "/openim")
public class OpenImInfoController extends BaseController {

    @Autowired
    private UsersService usersService;

    @Value("${openim.ws}")
    private String openimWs = ""; //请求地址
    @Value("${openim.platform}")
    private String openimPlatform = ""; //平台

    String htmlUrl = "/openim/";

    /**
     * 即时通讯
     * @return
     */
    @RequestMapping("index")
    public String Index() {
        return htmlUrl + "Index.html";
    }

    /**
     * 检测主机连通性
     * @param id awx中作业id
     * @param hostId 主机id
     * @return InterfaceResult
     */
    @GetMapping(value = "getopenimconfig")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onGetOpenimConfig() {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        List<UserLogin> listUserLoginByUserId = usersService.findUserLoginByUserId(SecurityUserHolder.getCurrentUserid());
        String tokenRefreshRes = OpenImApi.getInstance().tokenRefresh(listUserLoginByUserId.get(0).getLoginAccount(), StringsUtil.getRandomCode(16,"2"));

        if (StringsUtil.isEmpty(tokenRefreshRes)) {
            result.setError("请求openim获取token失败！");
            return result;
        }
        JSONObject jsonObject = JSONObject.parseObject(tokenRefreshRes);
        int errregisterCode = Integer.valueOf(jsonObject.getString("errCode"));
        if (errregisterCode != 0) { // 注册openim失败
            result.setError("openim获取token失败!："+jsonObject.getString("errMsg"));
            return result;
        }
        String data = jsonObject.getString("data");
        if (StringsUtil.isEmpty(data)) {
            result.setError("请求openim获取token返回数据缺失！");
            return result;
        }
        //注册成功获取返回token
        JSONObject dataJsonObject = JSONObject.parseObject(data);
        String token = dataJsonObject.getString("token");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("userId", listUserLoginByUserId.get(0).getLoginAccount());
        resultMap.put("token", token);
        resultMap.put("openimWs", openimWs);
        resultMap.put("openimPlatform", openimPlatform);
        result.setMsg("获取用户登录所需信息成功!");
        result.setResult(resultMap);
        return result;
    }
}
